package utils

import (
	"crypto/md5"
	"encoding/hex"
	"golang.org/x/crypto/bcrypt"
	"regexp"
	"strings"
)

// MD5加密

func GetMD5Encode(str string) string {
	h := md5.New()
	h.Write([]byte(str))
	return hex.EncodeToString(h.Sum(nil))
}

// HashPassword 返回加密的密码
func HashPassword(rawPwd string) string {
	//初始密码123456
	hash, _ := bcrypt.GenerateFromPassword([]byte(rawPwd), bcrypt.DefaultCost) //加密处理

	return string(hash) // 保存在数据库的密码，每次生成都不同
}

// PasswordCheck 验证密码
func PasswordCheck(rawPwd string, encodePwd string) error {
	return bcrypt.CompareHashAndPassword([]byte(encodePwd), []byte(rawPwd))
}

func PhoneNumCheck(phone string) bool {
	pattern := "^1[3-9]\\d{9}$"
	matched, _ := regexp.MatchString(pattern, phone)
	return matched
}

func FileNameFilter(name string) string {
	words := strings.Split(name, "_")
	for i := 1; i < len(words); i++ {
		words[i] = strings.ToUpper(words[i][0:1]) + words[i][1:]
	}

	camelCaseStr := strings.Join(words, "")
	return camelCaseStr
}
